import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { Form, Select, Modal, message } from 'antd';
import { dictionaryList } from '@/services/dictionary';
import { updateOfferState } from '@/services/myRecruit/offerDetail';

const { Option } = Select;
const { Item } = Form;

const StatusForm = ({
    form,
    offerId,
    visible,
    onOk,
    candidateCode,
    offerState,
    ...rest
}) => {
    const { getFieldDecorator } = form;
    const [loading, setLoading] = useState(false);
    const [statusList, setStatusList] = useState([]);

    const handleOk = () => {
        form.validateFieldsAndScroll(async (err, values) => {
            if (!err) {
                setLoading(true);
                const response = await updateOfferState({
                    candidateCode,
                    offerState: values.status,
                    id: offerId
                });

                if (response.code === 10000) {
                    setLoading(false);
                    onOk();
                    message.success('设置成功！');
                } else {
                    message.error(response.msg);
                }
            }
        });
    };

    useEffect(() => {
        const fetchDict = async () => {
            if (visible && candidateCode) {
                const response = await dictionaryList({
                    code: 'offerState'
                });

                if (response.code === 10000) {
                    // 过滤掉'入职中'
                    setStatusList(response.data);
                }
            }
        };
        fetchDict();
    }, [visible, candidateCode]);
    return (
        <Modal
            visible={visible}
            onOk={handleOk}
            confirmLoading={loading}
            {...rest}
        >
            <Form layout="inline">
                <Item label="请选择offer状态">
                    {getFieldDecorator('status', {
                        initialValue: offerState,
                        rules: [
                            {
                                required: true,
                                message: '请选择offer状态'
                            }
                        ]
                    })(
                        <Select style={{ width: 300 }}>
                            {statusList.map(status => (
                                <Option
                                    key={status.dictValue}
                                    value={status.dictValue}
                                    disabled={status.dictValue === 'beingEntry'}
                                >
                                    {status.dictName}
                                </Option>
                            ))}
                        </Select>
                    )}
                </Item>
            </Form>
        </Modal>
    );
};

StatusForm.propTypes = {
    candidateCode: PropTypes.string.isRequired,
    offerId: PropTypes.number.isRequired,
    offerState: PropTypes.string.isRequired
};

export default Form.create()(StatusForm);
